let map_center = [108.002878,30.274886];
let map_zoom = 6;
let map_enable = false;
let map_adcode = 500000;
//------------------------------------ 热力图 ------------------------------------------
var heatdata = [];
/**------------------------------------ axios数据 -------------------------------*/
axios
    .get('/d/broadcast/heat')
    .then(function (response) {
        let location_list = response.data.location_list;
        for (let item in location_list) {
            if (!location_list.hasOwnProperty(item)) continue;
            let location = location_list[item].split(',');
            heatdata.push({
                lng: location[0],
                lat: location[1],
                count: 10
            })
        }
        setheat();
    });

var map = new AMap.Map("container", {
    zoom: map_zoom,//地图级别
    // center: [112.935739,34.049629],//地图中心
    center: map_center,
    mapStyle: 'amap://styles/blue',
    rotateEnable:map_enable,
    dragEnable:map_enable,
    scrollWheel:map_enable,
    doubleClickZoom:map_enable
});
/**----------- 行政区浏览 ------------------*/
AMapUI.load(['ui/geo/DistrictExplorer', 'lib/$'], function (DistrictExplorer, $) {
    //创建一个实例
    let districtExplorer = window.districtExplorer = new DistrictExplorer({
        eventSupport: true, //打开事件支持
        map: map
    });
    let node_all;
    //当前聚焦的区域
    let currentAreaNode = null;

    let $tipMarkerContent = $('<div style="color:white;font-size:3px"></div>');

    let tipMarker = new AMap.Marker({
        content: $tipMarkerContent.get(0),
        offset: new AMap.Pixel(0, 0),
        bubble: true,
        zIndex: 99
    });

    //根据Hover状态设置相关样式
    function toggleHoverFeature(feature, isHover, position) {

        tipMarker.setMap(isHover ? map : null);

        if (!feature) {
            return;
        }

        let props = feature.properties;

        if (isHover) {

            //更新提示内容
            $tipMarkerContent.html(props.name);
            //更新位置
            tipMarker.setPosition(position || props.center);
        }

        $('#area-tree').find('h2[data-adcode="' + props.adcode + '"]').toggleClass('hover', isHover);

        //更新相关多边形的样式
        let polys = districtExplorer.findFeaturePolygonsByAdcode(props.adcode);
        for (let i = 0, len = polys.length; i < len; i++) {

            polys[i].setOptions({
                fillOpacity: isHover ? 0.5 : 0.2
            });
        }
    }

    //监听feature的hover事件
    districtExplorer.on('featureMouseout featureMouseover', function (e, feature) {
        toggleHoverFeature(feature, e.type === 'featureMouseover',
            e.originalEvent ? e.originalEvent.lnglat : null);
    });

    //监听鼠标在feature上滑动
    districtExplorer.on('featureMousemove', function (e) {
        //更新提示位置
        tipMarker.setPosition(e.originalEvent.lnglat);
    });

    //绘制某个区域的边界
    function renderAreaPolygons(areaNode) {
        node_all = areaNode;
        //清除已有的绘制内容
        districtExplorer.clearFeaturePolygons();

        //绘制子区域
        districtExplorer.renderSubFeatures(areaNode, function () {

            // let fillColor = colors[i % colors.length];
            // let strokeColor = colors[colors.length - 1 - i % colors.length];
            let strokeColor = "#FFFFFF";

            return {
                cursor: 'default',
                bubble: true,
                strokeColor: strokeColor, //线颜色
                strokeOpacity: 1, //线透明度
                strokeWeight: 1, //线宽
                fillColor: null, //填充色
                fillOpacity: 0.35, //填充透明度
            };
        });

        //绘制父区域
        districtExplorer.renderParentFeature(areaNode, {
            cursor: 'default',
            bubble: true,
            strokeColor: '#FFFFFF', //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 1, //线宽
            fillColor: null, //填充色
            fillOpacity: 0.35, //填充透明度
        });
    }

    //切换区域后刷新显示内容
    function refreshAreaNode(areaNode) {

        districtExplorer.setHoverFeature(null);

        renderAreaPolygons(areaNode);

        //更新选中节点的class
        let $nodeEles = $('#area-tree').find('h2');

        $nodeEles.removeClass('selected');

        let $selectedNode = $nodeEles.filter('h2[data-adcode=' + areaNode.getAdcode() + ']').addClass('selected');

        //展开下层节点
        $selectedNode.closest('li').removeClass('hide-sub');

        //折叠下层的子节点
        $selectedNode.siblings('ul.sublist').children().addClass('hide-sub');
    }

    //切换区域
    function switch2AreaNode(adcode, callback) {

        if (currentAreaNode && ('' + currentAreaNode.getAdcode() === '' + adcode)) {
            return;
        }

        loadAreaNode(adcode, function (error, areaNode) {

            if (error) {

                if (callback) {
                    callback(error);
                }

                return;
            }

            currentAreaNode = window.currentAreaNode = areaNode;

            //设置当前使用的定位用节点
            districtExplorer.setAreaNodesForLocating([currentAreaNode]);

            refreshAreaNode(areaNode);

            if (callback) {
                callback(null, areaNode);
            }
        });
    }

    //加载区域
    function loadAreaNode(adcode, callback) {

        districtExplorer.loadAreaNode(adcode, function (error, areaNode) {

            if (error) {

                if (callback) {
                    callback(error);
                }

                console.error(error);

                return;
            }

            if (callback) {
                callback(null, areaNode);
            }
        });
    }

    //重庆
    switch2AreaNode(map_adcode);
});
// let layer = new Loca.HeatmapLayer({
//     map: map,
// });
var heatmap;

function setheat() {
    //map为高德地图实例，AMap.Heatmap为高德地图热力图插件
    map.plugin(["AMap.Heatmap"], function () {
        heatmap = new AMap.Heatmap(map, {
            radius: 20, //给定半径
            opacity: [0, 0.9],
            gradient: {
                0.5: '#2c7bb6',
                0.65: '#abd9e9',
                0.7: '#ffffbf',
                0.9: '#fde468',
                1.0: '#d7191c'
            }
        });
        heatmap.setDataSet({data: heatdata});
    });
}


function ResetHeat() {
    heatmap.setDataSet({data: heatdata});
}

//------------------------------------ 图层蒙板 ------------------------------------------
var colors = {};
var getColorBySoc = function (adcode) {
    if (adcode) {
        if (!colors[adcode]) {
            if (adcode === map_adcode) {
                return null;
            }
            return '#033447'
            // return '#FFFFFF'
        }
    } else {
        return null
    }
};
var disCountry = new AMap.DistrictLayer.Country({
    zIndex: 19,
    SOC: 'CHN',
    depth: 1,
    styles: {
        'fill': function (props) {
            return getColorBySoc(props.adcode_pro)
        }
    }
});
map.add(disCountry);

map.on("moveend",function(e){
    console.log(map.getCenter());
});

// ************* 缩放获取当前地图缩放级别 ******************
map.on("zoomend",function(){
    console.log(map.getZoom());
});















